package com.tz.movie.controller;



import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.tz.movie.domain.Place;
import com.tz.movie.domain.PlaceExample;
import com.tz.movie.domain.status;
import com.tz.movie.mapper.PlaceMapper;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestBody;

@RestController
@RequestMapping("/place")
public class PlaceController {
  @Autowired
  PlaceMapper Placemapper;

  @PostMapping(value = "/getAllPlace")
  public String getAllUser(@RequestBody JSONObject jsonparm, HttpServletRequest request, HttpSession session) {
    JSONObject result = new JSONObject();
    List<Place> Places = Placemapper.selectByExample(new PlaceExample());
    JSONArray jsonArray = new JSONArray();
    for (Place Place : Places) {
      JSONObject obj = JSONObject.parseObject(Place.toString());
      jsonArray.add(obj);
    }
    result.put("result", status.s_ok);
    result.put("data", jsonArray);
    return result.toJSONString();
  }

  @PostMapping(value = "/addPlace")
  public String addPlace(@RequestBody JSONObject jsonparm, HttpServletRequest request, HttpSession session) {

    JSONObject result = new JSONObject();
    String name = jsonparm.getString("name");
    String info = jsonparm.getString("info");
    int siterows=jsonparm.getInteger("siterows");
    int sitecol=jsonparm.getInteger("sitecol");
    Place Place = new Place();

    Place.setName(name);
    Place.setInfo(info);
    Place.setSiterows(siterows);
    Place.setSitecol(sitecol);
    Placemapper.insert(Place);
    result.put("result", status.s_ok);
    return result.toJSONString();
  }

  
  @PostMapping(value = "/updatePlace")
  public String updatePlace(@RequestBody JSONObject jsonparm, HttpServletRequest request, HttpSession session) {
    JSONObject result = new JSONObject();
    int id = jsonparm.getInteger("id");

    Place Place = new Place();
    Place.setName(jsonparm.getString("name"));
    Place.setInfo(jsonparm.getString("info"));

    PlaceExample PlaceExample = new PlaceExample();
    PlaceExample.Criteria criteria = PlaceExample.createCriteria();
    criteria.andIdEqualTo(id);
    Placemapper.updateByExampleSelective(Place, PlaceExample);
    result.put("result", status.s_ok);
    return result.toJSONString();
  }
  
  @PostMapping(value = "/delPlace")
  public String delPlace(@RequestBody JSONObject jsonparm, HttpServletRequest request, HttpSession session) {
    JSONObject result = new JSONObject();

    int delid = jsonparm.getInteger("id");
    PlaceExample commentExample = new PlaceExample();
    PlaceExample.Criteria criteria=  commentExample.createCriteria();
    criteria.andIdEqualTo(delid);
    Placemapper.deleteByExample(commentExample);
    result.put("result", status.s_ok);

    return result.toJSONString();
  }
}